var bidId;
var balance;
var startMoney = 0;
var riseMoney = 0;
var limitMoney = 0;
var afterMoney =  0;
var Main = function() {
	mui.init({
		swipeBack: false //启用右滑关闭功能
	});
	this.init();
	this.even();
	this.loadData();
};
Main.prototype = {
	init: function() {
		bidId = Util.requestURL('bidId');
	},
	even: function() {
		var _this = this;
		$('#G-go-buy').on('tap', function() {
			_this.goBug();
		})
		
		/**跳转充值页面*/
		$('#btn-toRecharge').on('tap', function() {
			if(Util.checkLogin()){
				if(!isH5){
					mui.openWindow('recharge.html?balance=' + balance,'recharge');
				}else{
					window.location.href = 'recharge.html?balance=' + balance;
				}
			}else{
				mui.openWindow('login.html');
			}
				
			})
	},
	goBug: function() { //立即购买
		var buyMoney = $("#buyMoney").val();
		//校验用户账户金额信息
		var curUserInfo = Util.getLoginUser();
		var validateUrl = server_url + '/creditFlow/financingAgency/availableInvestMoneyPlBidPlan.do';
		var param = {
			memberId: curUserInfo.memberId
		}
		$.getJSON(validateUrl, param, function(retData) {
			if(retData.success) {
				if(buyMoney == '') {
					mui.toast('投标金额不能为空');
					$("#G-go-buy").removeClass('mui-active');
					return false;
				}
				if(parseFloat(buyMoney) > parseFloat(limitMoney)) {
					mui.toast('投标金额不能大于最大投资金额');
					$("#G-go-buy").removeClass('mui-active');
					return false;
				}
				if(parseFloat(buyMoney) < parseFloat(startMoney)) {
					$("#G-go-buy").removeClass('mui-active');
					mui.toast('投标金额不能小于起投金额');
					return false;
				}
				if(parseFloat(buyMoney) > parseFloat(retData.availableInvestMoney)) {
					$("#G-go-buy").removeClass('mui-active');
					mui.toast('投标金额不能大于账户余额');
					return false;
				}
				if(parseFloat(buyMoney) > parseFloat(afterMoney)) {
					$("#G-go-buy").removeClass('mui-active');
					mui.toast('投标金额不能大于可投金额');
					return false;
				}

				/*var buyUrl = server_url+ '/creditFlow/financingAgency/bidingPlBidInfo.do?isMobile=1'+
            	                 '&plBidInfo.bidId='+bidId+
            					 '&plBidInfo.userMoney='+buyMoney+
            					 '&plBidInfo.userName='+curUserInfo.loginName+
                                 '&plBidInfo.userId='+curUserInfo.memberId+
            	                 '&currM='+retData.availableInvestMoney+
            	                 '&sign_Money='+buyMoney;
            	            buyUrl+='&accountid='+curUserInfo.memberId;
            	            Util.getThirdPayLink(buyUrl);*/
				var url = server_url + '/creditFlow/financingAgency/bidingPlBidInfo.do';
				var param = {
					"isMobile": isMobile,
					"plBidInfo.bidId": bidId,
					"plBidInfo.userMoney": buyMoney,
					"plBidInfo.userName": curUserInfo.loginName,
					"plBidInfo.userId": curUserInfo.memberId,
					"currM": retData.availableInvestMoney,
					"sign_Money": buyMoney,
					"plBidInfo.bidId": bidId,
					"accountid": storage.memberId
				}
				url = UrlHelper.customUrl(UrlHelper.urlEncode(param), url);
				url = encodeURIComponent(url);
				mui.openWindow({"url":"huifu.html?url=" + url + "&module=biding","id":"huifu-page"});
			}
		});
	},
	loadData: function() { //加载数据
		//账户余额
		var url = server_url + '/user/mobileaccountBpCustMember.do';
		var param = {
			isMobile: '1',
			memberId: Util.getLoginUser().memberId
		}
		$.getJSON(url, param, function(retData) {
			if(retData.success) {
				balance = retData.money;
				$('.m-available-money').html(retData.money + '元');
			}
		});
		
		var url1 = server_url + '/creditFlow/financingAgency/bidPlanDetailPlBidPlan.do';
		var param1 = {
					isMobile:1,
					bidId:bidId
					};
		//加载数据
		$.getJSON(url1,param1,function(retData){
			var data = retData.data;
			startMoney = data.startMoney;//起投金额
            riseMoney = data.riseMoney;//递增金额
            limitMoney = data.maxMoney;//上限金额
            afterMoney = data.afterMoney;//可投金额
            
            //获取起投金额信息
			$('.m-start-money').html(startMoney + '元');
			$('.m-rise-money').html(riseMoney + '元');
			$('.m-limit-money').html(limitMoney + '元');
			$('.m-after-money').html(afterMoney + '元');
		});
			

		
	}
};